package com.wzy.Controller.admin;

import com.wzy.entity.Violence;
import com.wzy.service.ViolenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Set;

/**
 * @author : Zht
 * @create : 2021/2/22
 * @describe:
 */
@Controller
@RequestMapping("/admin")
public class ViolencesController {

    @Autowired
    private ViolenceService violenceService;

    @Autowired
    private RedisTemplate redisTemplate;

    //日寇暴行列表
    @GetMapping("/violence")
    public ModelAndView index(Model model, @RequestParam(value = "page", defaultValue = "1") Integer page, Violence condition) {
        return new ModelAndView("admin/violence/violences")
                .addObject("pager", this.violenceService.list(condition, page))
                .addObject("condition", condition);
    }

    //跳到新增暴行
    @GetMapping("/violence/input")
    public String input() {
        return "admin/violence/violences-input";
    }

    //新增暴行
    @PostMapping("/violence")
    public String append(Violence violence, RedirectAttributes attributes, Model model, @RequestParam(value = "page", defaultValue = "1") Integer page, Violence condition) {

        if (condition != null && condition.getImg() == null) {
            model.addAttribute("pager", this.violenceService.list(condition, page));
            model.addAttribute("condition", condition);
            return "admin/violence/violences";
        }
        //请缓存
        Set<String> keys = redisTemplate.keys("*violence*");
        redisTemplate.delete(keys);
        if (this.violenceService.append(violence)) {
            attributes.addFlashAttribute("messages", "添加成功");
        } else {
            attributes.addFlashAttribute("messages", "添加失败");
        }
        return "redirect:/admin/violence";
    }

    //跳到编辑暴行
    @GetMapping("/violence/{violenceId}/input")
    public String edit(@RequestParam("violenceId") Long violenceId, Model model) {
        model.addAttribute("violence", this.violenceService.detail(violenceId));
        return "admin/violence/violences-modify";
    }

    //编辑暴行
    @PostMapping("/violence/{violenceId}")
    public String editPost(Violence violence, RedirectAttributes attributes) {
        Set<String> keys = redisTemplate.keys("*violence*");
        redisTemplate.delete(keys);
        if (this.violenceService.modify(violence)) {
            attributes.addFlashAttribute("messages", "编辑成功");
        } else {
            attributes.addFlashAttribute("messages", "编辑失败");
        }
        return "redirect:/admin/violence";
    }

    //删除暴行
    @RequestMapping("/violence/delete")
    public String delete(Long violenceId, RedirectAttributes attributes) {
        Set<String> keys = redisTemplate.keys("*violence*");
        redisTemplate.delete(keys);
        this.violenceService.remove(violenceId);
        attributes.addFlashAttribute("messages", "删除成功");
        return "redirect:/admin/violence";
    }

}
